package com.szxydb.dmp.admin.system.controller;

import com.szxydb.dmp.admin.system.model.SystemAdmin;
import com.szxydb.dmp.admin.system.model.ValidateCode;
import com.szxydb.dmp.admin.system.request.SystemAdminLoginRequest;
import com.szxydb.dmp.admin.system.request.SystemAdminRequest;
import com.szxydb.dmp.admin.system.response.SystemAdminResponse;
import com.szxydb.dmp.admin.system.response.SystemGroupDataAdminLoginBannerResponse;
import com.szxydb.dmp.admin.system.service.SystemAdminService;
import com.szxydb.dmp.admin.system.service.SystemConfigService;
import com.szxydb.dmp.admin.system.service.SystemGroupDataService;
import com.szxydb.dmp.admin.system.service.ValidateCodeService;
import com.szxydb.dmp.common.CheckAdminToken;
import com.szxydb.dmp.common.CommonPage;
import com.szxydb.dmp.common.CommonResult;
import com.szxydb.dmp.common.PageParamRequest;
import com.szxydb.dmp.constants.Constants;
import com.szxydb.dmp.exception.CrmebException;
import com.szxydb.dmp.exception.ExceptionCodeEnum;
import com.szxydb.dmp.utils.CrmebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("api/admin")
@Api(tags = "Admin 平台用户")
public class AdminUserController {

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SystemAdminService systemAdminService;

    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private SystemGroupDataService systemGroupDataService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    /**
     * 后台登录
     * @param systemAdminLoginRequest
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "后台登录")
    @PostMapping(value = "/login",produces = "application/json")
    public CommonResult<SystemAdminResponse> SystemAdminLogin(@RequestBody @Validated SystemAdminLoginRequest systemAdminLoginRequest, HttpServletRequest request) throws Exception{
        ValidateCode validateCode = new ValidateCode(systemAdminLoginRequest.getKey(),systemAdminLoginRequest.getCode());
        boolean codeCheckResult = validateCodeService.check(validateCode);
        if (!codeCheckResult) throw  new CrmebException("验证码不正确");
        SystemAdminRequest systemAdminRequest = new SystemAdminRequest();
        systemAdminRequest.setAccount(systemAdminLoginRequest.getAccount());
        systemAdminRequest.setPwd(systemAdminLoginRequest.getPwd());
        SystemAdminResponse systemAdminResponse = systemAdminService.login(systemAdminRequest, CrmebUtil.getClientIp(request));
        if (null == systemAdminResponse){
            return CommonResult.failed(ExceptionCodeEnum.FAILED,"login failed");
        }

        if (StringUtils.isNoneBlank(systemAdminLoginRequest.getWxCode())){
            systemAdminService.bind(systemAdminLoginRequest.getWxCode(),systemAdminRequest.getId());
        }
        return CommonResult.success(systemAdminResponse,"login success");
    }

    /**
     * 获取登录页图片
     * @return
     */
    @ApiOperation(value = "获取登录页图片")
    @RequestMapping(value = "/getLoginPic",method = RequestMethod.GET)
    public CommonResult<Map<String,Object>> getLoginPic(){
        Map<String,Object> map = new HashMap<>();
        //背景图
        map.put("backgroundImage",systemConfigService.getValueByKey(Constants.CONFIG_KEY_ADMIN_LOGIN_BACKGROUND_IMAGE));
        map.put("logo",systemConfigService.getValueByKey(Constants.CONFIG_KEY_ADMIN_LOGIN_LOGO));
        List<SystemGroupDataAdminLoginBannerResponse> bannerList = systemGroupDataService.getListByGid(Constants.GROUP_DATA_ID_ADMIN_LOGIN_BANNER_IMAGE_LIST,SystemGroupDataAdminLoginBannerResponse.class);
        map.put("banner",bannerList);
        return CommonResult.success(map);
    }

    /**
     * 退出登录
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "后台退出登录")
    @GetMapping(value = "/logout")
    public CommonResult<SystemAdminResponse> SystemAdminLogout(HttpServletRequest request) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        systemAdminService.logout(token);
        return CommonResult.success("logout success");
    }


    /**
     * 通过token获取用户信息
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "通过token获取用户信息")
    @GetMapping(value = "/getAdminInfoByToken")
    public CommonResult<SystemAdminResponse> getAdminInfo(HttpServletRequest request)throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SystemAdminResponse systemAdminResponse = systemAdminService.getInfoByToken(token);
        return CommonResult.success(systemAdminResponse);
    }


}
